import Vue from 'vue'
import VueRouter from 'vue-router'
//引入vuex
import store from '@/store'
import { Message } from 'element-ui'  //导入element-ui组件库

Vue.use(VueRouter)

/* Layout */
import Layout from '@/views/Home'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login'),
    hidden: true,
    meta: { title: '登录' }
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    children: [{
      path: 'index',
      name: 'Index',
      component: () => import('@/views/index/index'),
      //isAuth 判断是否需要登录条件
      meta: { title: '首页', isAuth: true, icon: "el-icon-menu" }
    }]
  },
  {
    path: '/information',
    component: Layout,
    redirect: '/information/index',
    children: [{
      path: 'index',
      name: 'Information',
      component: () => import('@/views/information/index'),
      //isAuth 判断是否需要登录条件
      meta: { title: '房源信息管理', isAuth: true, icon: "el-icon-s-order" }
    }]
  },
  {
    path: '/housing',
    component: Layout,
    redirect: '/housing/index',
    children: [{
      path: 'index',
      name: 'Housing',
      component: () => import('@/views/housing/index'),
      //isAuth 判断是否需要登录条件
      meta: { title: '租赁信息管理', isAuth: true, icon: "el-icon-s-finance" }
    }]
  },
  {
    path: '/breakdown',
    component: Layout,
    redirect: '/breakdown/index',
    children: [{
      path: 'index',
      name: 'Breakdown',
      component: () => import('@/views/breakdown/index'),
      //isAuth 判断是否需要登录条件
      meta: { title: '故障维修管理', isAuth: true, icon: "el-icon-setting" }
    }]
  },
  {
    path: '/reservation',
    component: Layout,
    redirect: '/reservation/index',
    children: [{
      path: 'index',
      name: 'Reservation',
      component: () => import('@/views/reservation/index'),
      //isAuth 判断是否需要登录条件
      meta: { title: '预约看房', isAuth: true, icon: "el-icon-s-marketing" }
    }]
  },
  {
    path: '/news',
    component: Layout,
    redirect: '/news/index',
    children: [{
      path: 'index',
      name: 'News',
      component: () => import('@/views/news/index'),
      //isAuth 判断是否需要登录条件
      meta: { title: '公告管理', isAuth: true, icon: "el-icon-document" }
    }]
  },
  {
    path: '/message',
    component: Layout,
    redirect: '/message/index',
    children: [{
      path: 'index',
      name: 'Message',
      component: () => import('@/views/message/index'),
      //isAuth 判断是否需要登录条件
      meta: { title: '留言管理', isAuth: true, icon: "el-icon-chat-line-round" }
    }]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/index',
    children: [{
      path: 'index',
      name: 'User',
      component: () => import('@/views/user/index'),
      //isAuth 判断是否需要登录条件
      meta: { title: '用户管理', isAuth: true, icon: "el-icon-s-custom" }
    }]
  },
  {
    path: '/mine',
    component: Layout,
    redirect: '/mine/index',
    children: [{
      path: 'index',
      name: 'Mine',
      component: () => import('@/views/mine/index'),
      //isAuth 判断是否需要登录条件
      meta: { title: '个人中心', isAuth: true }
    }]
  },
]

const router = new VueRouter({
  routes
})


//全局前置路由，to:即将要进入的目标路由对象,from:当前导航即将要离开的路由对象
router.beforeEach((to, from, next) => {
  //动态设置页面标题
  document.title = to.meta.title ? to.meta.title + '-房屋租赁管理系统' : '房屋租赁管理系统'
  //next就是执行的意思，不写页面是不会跳转的
  // console.log(to, from, '0000000');
  if (to.meta.isAuth) {     //判断是否需要登录
    if (store.state.token) {
      next();
    } else {
      Message({
        message: "请先登录",
        type: "error",
      });
      next({
        path: '/login',
        // 将跳转的路由path作为参数，登录成功后跳转到该路由
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})


export default router
